package net.easyjava.admin.controller;

import net.easyjava.base.manager.ArticleManager;
import net.easyjava.tools.model.ModelBean;
import net.easyjava.tools.model.ModelConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by wenxiaoning on 15/12/18.
 */
@Controller
@RequestMapping("/admin/system")
public class SystemController extends BaseController {
    private  Logger logger = LoggerFactory.getLogger(this.getClass().getName());
    @Resource
    private ArticleManager articleManager;


    /**
     * 缓存列表
     * @PARAM REQUEST
     * @PARAM RESPONSE
     * @RETURN
     */
    @RequestMapping("/list.html")
    @ResponseBody
    public ModelAndView list(
            HttpServletRequest request, HttpServletResponse response) {
        String message = ModelConstants.MESSAGE_ERROR;
        try {

            return new ModelAndView("admin/system/list.html");
        }catch (Exception e){
            logger.error(e.getMessage(),e.fillInStackTrace());
            return null;
        }

    }

    /**
     * 刷新缓存
     * @param type
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/reset_cache.json")
    @ResponseBody
    public ModelBean resetCache(
            @RequestParam String type,
            HttpServletRequest request, HttpServletResponse response) {
        String message = ModelConstants.MESSAGE_ERROR;
        ModelBean modelBean = new ModelBean();
        try {
            if("all".equalsIgnoreCase(type)){
                articleManager.resetCache();
            }else if("article".equalsIgnoreCase(type)){
                articleManager.resetCache();
            }

        }catch (Exception e){
            logger.error(e.getMessage(),e.fillInStackTrace());
            modelBean = new ModelBean(message);
        }
        return model(modelBean,request,response);
    }






}
